package com.omesoft.hksnore.util.omeview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.omesoft.hksnore.util.L;
import com.omesoft.hksnore.util.dao.MendaleSleepIfcImpl;
import com.omesoft.hksnore.util.data.MyDateUtil;
import com.omesoft.hksnore.util.data.MySharedPreferencesUtil;
import com.omesoft.hksnore.util.entity.Medix_Pub_Sync_SleepDetail;

import java.util.List;


/**
 * Created by BigFace on 2017/5/16.
 */

public class SeekBarDrawble extends Drawable {

    private Paint paint;
    private List<Medix_Pub_Sync_SleepDetail> detailList;
    private MendaleSleepIfcImpl mifc;
    private String SleepID;
    private Context context;
    private long endTime;
    private long startTime;
    private long totalTime;//总时长分钟数

    RectF rect;

    public SeekBarDrawble(Context context, String sleepid) {
        this.SleepID = sleepid;
        this.context = context;
        paint = new Paint();
        rect = new RectF();
        init();
    }

    private void init() {
        mifc = new MendaleSleepIfcImpl(context);
        detailList = mifc.findAllSleepDetaiBySleepId(SleepID);
        startTime = MyDateUtil.stringTOlongDate(MySharedPreferencesUtil.getTempStartTime(context));
        endTime = MyDateUtil.stringTOlongDate(MySharedPreferencesUtil.getTempEndTime(context));
        paint.setAntiAlias(true);//抗锯齿
        paint.setShader(null);
        paint.setColor(Color.parseColor("#E9E1C4"));
        totalTime = (endTime - startTime) / 60;
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        final Rect bounds = getBounds();
        paint.setStrokeWidth(bounds.height());

        if (detailList != null && detailList.size() > 0) {
            long tempTime = 0;
            long tenMIN = 600;//十分钟
            for (int i = 0; i < detailList.size(); i++) {
                if (detailList.get(i).getSnoreCount() > 0) {
                    long snoreStart = MyDateUtil.stringTOlongDate(detailList.get(i).getCreatedDate());
                    float initPosition = (((snoreStart - startTime) / 60F) / totalTime) * bounds.width();
                    float endPosition = (10f / totalTime) * bounds.width();
                    L.e("detailList  " + initPosition + "   " + endPosition);
                    if (snoreStart < endTime) {
                        if (snoreStart > tempTime) {
                            if (snoreStart + endPosition > bounds.width()) {

                                canvas.drawLine(initPosition, bounds.centerY(), initPosition + endPosition, bounds.centerY(), paint);
                            } else {

                                canvas.drawLine(initPosition, bounds.centerY(), bounds.width(), bounds.centerY(), paint);
                            }
                            tempTime = snoreStart + tenMIN;

                        }
                    }

                }


            }
        }


        //绘制圆角矩形


    }


    @Override
    public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        paint.setColorFilter(colorFilter);

    }

    @Override
    public int getOpacity() {
        return PixelFormat.UNKNOWN;
    }

    public static class Builder {
        /**
         * 分割段数
         */
        String sleepID;

        Context context;

        public Builder setSleepID(String id) {
            sleepID = id;
            return this;
        }

        public Builder setContext(Context context) {
            this.context = context;
            return this;
        }

        /**
         * Creates a new SeekBarDrawble with the requested parameters
         *
         * @return new SeekBarDrawbleInstance
         */
        public SeekBarDrawble create() {
            return new SeekBarDrawble(context, sleepID);
        }
    }

}
